<?php

Class Mod_students extends Model {

    function Mod_students() {
        parent::Model();
    }
    
  //================================this is function  for using adding new data of user==================//
    
    function add() {
        $email_name = $this->input->post('txt_mail');
        // echo  $email_name;die();
        $query_student = $this->db->query('SELECT *FROM tbl_student');
        foreach ($query_student->result() as $rows) {
            $stud_email = $rows->stu_email;
            if ($email_name == $stud_email) {
                $this->session->set_userdata('invalid_mail', '<p style="color:red;">Can not insert email with the same email!</p>');
                redirect('students/add');
            }
        }
        
        //==================this is array in order to insert into database in tbl_user=================//
        
        $data = array(
            'stu_name' => $this->input->post('txt_studentname'),
            'stu_sex' => $this->input->post('studentsex'),
            'stu_dob' => $this->input->post('text_dob'),
            'stu_email' => $this->input->post('txt_mail'),
            // 'stu_date'=>mdate('%Y-%m-%d %h:%i:%s %A'),
            'stu_telephone' => $this->input->post('txt_phone'),
            'stu_mailist' => $this->input->post('mail'),
            'stu_type_id' => $this->input->post('stud_type'),
            'stu_get_information' => $this->input->post('stu_type'),
            'stu_des' => $this->input->post('txt_description'),
            'stu_status'=>1,
        );
        if ($this->db->insert('tbl_student', $data))
            return true;
        else
            return false;
    }
  //======================this is function for using view pagination=================//
    
    function select_view() {
        $this->db->select('*');
        return $this->db->get('tbl_student');
        
    }
   //======================this is function for using pagination===================//
    
    function get_page_limit($start_row, $per_page) {
        $query = "SELECT *FROM tbl_student limit $start_row,$per_page";
        $result = $this->db->query($query);
        return $result;
    }
    
   //======================this is function for using edit user ==================//
    
    function edit() {
        $data = array(
            'stu_name' => $this->input->post('txt_studentname'),
            'stu_sex' => $this->input->post('studentsex'),
            'stu_dob' => $this->input->post('text_dob'),
            'stu_email' => $this->input->post('txt_mail'),
            // 'stu_date'=>mdate('%Y-%m-%d %h:%i:%s %A'),
            'stu_telephone' => $this->input->post('txt_phone'),
            'stu_mailist' => $this->input->post('mail'),
            'stu_type_id' => $this->input->post('stud_type'),
            'stu_get_information' => $this->input->post('stu_type'),
            'stu_des' => $this->input->post('txt_description'),
        );
        $this->db->where('stu_id', $this->uri->segment(3));
        if ($this->db->update('tbl_student', $data))
            return true;
        else
            return false;
    }

    //=========================this is function is for selecting edit=======================//
    
    function select_edit() {
        $foredit = $this->db->where('stu_id', $this->uri->segment('3'));
        return $this->db->get('tbl_student');
    }

    //function for selecting view data
    
    function viewdata() {
        $this->db->select('*');
        $this->db->from('tbl_student');
        $this->db->join('tbl_stu_type', 'tbl_stu_type.stu_type_id=tbl_student.stu_type_id');
        $this->db->where('stu_id', $this->uri->segment('3'));
        return $this->db->get();
    }

    //===========================this is function for using deleted user==================//
    
    function delete() {
        $seg = $this->uri->segment('3');
        // $this->db->select('*');
        $this->db->where('stu_id', $seg);
        $this->db->delete('tbl_student');
    }
    
    //=================this is function for searching============================//
    
     function get_teachers($p=null) { // $p is array
        if ($this->input->post($this->dbf->stu_name) && $this->input->post($this->dbf->stu_name) != '')
        $this->db->like($this->dbf->stu_name, $this->input->post($this->dbf->stu_name));
        $this->db->limit($p[0], $p[1]);
        $this->db->where('stu_status',1);
        $this->db->order_by('stu_id','desc');
        return $this->db->get('tbl_student');
    }

    // }
}

?>